<?php

define('PAGE_ID',3);

require_once 'init.php';
require_once 'projects_common.php';

$url = $page->link();

$clientId = 0;
if ($par->exist('clientId')) 
{
	$clientId = $par->get('clientId');
	if (!is_numeric($clientId))
	{
		$clientId = query2var("SELECT id FROM clients WHERE code='$clientId'");
	}
}

$catId = 0;
if ($par->exist('catId')) 
{
	$catId = $par->get('catId');
	if (!is_numeric($catId))
	{
		$catId = query2var("SELECT id FROM project_categories WHERE code='$catId'");
	}
}


$recCount = 0;

if ($catId > 0)
{
	$currentCat = findProjectCategory($catId);
	
	$pageName = $currentCat['name_full'];
	if (empty($pageName)) {
		$pageName = $currentCat['name'];
	}
	$pageTitle = $currentCat['title'];
	$pageText = $currentCat['txt'];
	$pageKeywords = $currentCat['keywords'];
	$pageDescription = $currentCat['descr'];
	
	$path[] = array(
		'name' => $page->menuName(),
		'link' => $page->link()
	);
	$path[] = array(
		'name' => $currentCat['name'],
		'link' => null
	);
	
	$recCount = countRecords('projects', array('category'=>$catId));
}
else if ($clientId > 0)
{
	$clientName = query2var("SELECT name FROM clients WHERE id=$clientId");

	$pageTitle = "$pageTitle - $clientName";
	$pageKeywords = "$pageKeywords $clientName";
	$pageDescription = "$pageDescription $clientName";
	
	$path[] = array(
		'name' => $page->menuName(),
		'link' => $page->link()
	);
	
	$path[] = array(
		'name' => $clientName,
		'link' => null
	);
	
	$recCount = countRecords('projects', array('client'=>$clientId));
}
else
{
	$path[] = array(
		'name' => $page->menuName(),
		'link' => null
	);
	
	$recCount = countRecords('projects', array());
}

$projects = array();

if ($recCount) 
{
	define('RECS_ON_PAGE', 8);
	define('PAGES_IN_LIST', 20);
	
	$ruler = new PageFormatter($recCount, RECS_ON_PAGE, PAGES_IN_LIST);
	$ruler->create();

	$whereSQL = '';
	if ($catId > 0)
	{
		$whereSQL = "WHERE category=$catId";
	}
	else if ($clientId > 0)
	{
		$whereSQL = "WHERE client=$clientId";
	}

	$projects = query2arrays("
		SELECT 
			IFNULL(project_categories.code,project_categories.id) AS cat_id,
			project_categories.name AS cat_name,
			project_categories.icon AS cat_icon,
			clients.id AS client_id,
			clients.name AS client_name,
			IFNULL(projects.code,projects.id) AS id,
			projects.name,
			YEAR(projects.dt) AS date_year,
			MONTH(projects.dt) AS date_month,
			projects.banner,
			projects.summary
		FROM projects
		INNER JOIN project_categories ON projects.category=project_categories.id
		INNER JOIN clients ON projects.client=clients.id
		$whereSQL
		ORDER BY projects.dt DESC, projects.sort_order ASC
		LIMIT {$ruler->getStartRec()}, {$ruler->getRecOnPage()}		
	");
	
	for ($i=0; $i<count($projects); ++$i)
	{
		$projects[$i]['date_month'] = getMonthName($projects[$i]['date_month']);
	}
	
	$pageNumbers = $ruler->visiblePages();
	$smarty->assign('pageNumbers',$pageNumbers);
	$smarty->assign('ruler', $ruler);
}


createSubmenu($catId);

$smarty->assign('projects',$projects);

display('projects');
?>